/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.herenit.beijing.poc.login;

import com.herenit.beijing.dh2011.security.entity.SecurityMenu;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.openide.DialogDisplayer;
import org.openide.LifecycleManager;
import org.openide.NotifyDescriptor;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.modules.ModuleInstall;
import org.openide.util.Exceptions;

public class Installer extends ModuleInstall {

    LoginForm form;
    private static final String ROOT_NODE = "Explorer";

    private void printFo(FileObject fo) {
        System.out.println(fo.getPath());
        if (fo.getChildren() != null && fo.getChildren().length > 0) {
            for (FileObject child : fo.getChildren()) {
                printFo(child);
            }
        }
    }

    @Override
    public void restored() {
        System.out.println("restored()");

        FileObject fo = FileUtil.getConfigRoot();
        printFo(fo);

        form = new LoginForm();
        createLoginDialog();
        // initFileSystem();
    }

    public void createLoginDialog() {


        JButton ok = new JButton();
        ok.setText("登陆");

        JButton cancel = new JButton();
        cancel.setText("重置");

        cancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
            }
        });

        ok.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                //   simpleAuthenticate();
            }
        });

        NotifyDescriptor nd = new NotifyDescriptor.Confirmation(form, "登陆界面");
        nd.setOptions(new Object[]{ok, cancel});
        DialogDisplayer.getDefault().notifyLater(nd);

        nd.addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                if (NotifyDescriptor.CLOSED_OPTION.equals(evt.getNewValue())) {
                    //nd. exit();
                }
            }
        });
    }

    private void simpleAuthenticate() {
        if ("admin".equals(form.getUserName()) && "admin".equals(form.getText())) {
            pass();
        } else {
            exit();
        }
    }

    private void authenticate() {
//        if (NbPreferences.forModule(UsermanagementPanel.class).get("userName", "").equals(this.form.getUsername())) {
//            try {
//                String storePassword = NbPreferences.forModule(UsermanagementPanel.class).get("passowrd", "");
//                String passwordMD5 = null;
//                MessageDigest md5 = MessageDigest.getInstance("MD5");
//                char[] passwordChar = form.getPassword();
//                byte[] tmp = new byte[passwordChar.length];
//                for (int i = 0; i < passwordChar.length; i++) {
//                    tmp[i] = (byte) passwordChar[i];
//                }
//                md5.update(tmp);
//                passwordMD5 = byteArrToString(md5.digest());
//
//                if (storePassword.equals(passwordMD5)) {
//                } else {
//                    exit();
//                }
//            } catch (NoSuchAlgorithmException ex) {
//                Exceptions.printStackTrace(ex);
//            }
//        } else {
//            exit();
//        }
    }

    private static String byteArrToString(byte[] b) {
        String res = null;
        StringBuilder sb = new StringBuilder(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            int j = b[i] & 0xff;
            if (j < 16) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(j));
        }
        res = sb.toString();
        return res.toUpperCase();
    }

    private void exit() {
        LifecycleManager.getDefault().exit();
    }

    private void pass() {
        this.close();
    }

    private void initFileSystem() {

        LoginService.getDefault().writeFileSystem(initData());
//        SwingUtilities.invokeLater(new Runnable() {
//
//            @Override
//            public void run() {
//                
//            }
//        });


    }

    private List<SecurityMenu> initData() {
        List<SecurityMenu> securityMenus = new ArrayList<SecurityMenu>();


        SecurityMenu securityMenu0 = new SecurityMenu();

        securityMenu0.setDisplayName(
                "根节点");
        securityMenu0.setIcon(
                "com/galileo/netbeans/module/explorer.png");
        securityMenu0.setPosition(
                100);
        securityMenus.add(securityMenu0);


        SecurityMenu securityMenu1 = new SecurityMenu();

        securityMenu1.setDisplayName(
                "mp3播放");
        securityMenu1.setIcon(
                "com/galileo/netbeans/module/player.png");
        securityMenu1.setPosition(
                200);
        securityMenus.add(securityMenu1);
        SecurityMenu securityMenu2 = new SecurityMenu();

        securityMenu2.setMenuPath(
                "Actions/Edit/com-galileo-netbeans-module-PlaylistAction");

        securityMenu2.setDisplayName(
                "播放列表");
        securityMenu2.setOriginalFile(
                "Actions/Edit/com-galileo-netbeans-module-PlaylistAction.instance");
        securityMenu2.setPosition(
                300);
        securityMenus.add(securityMenu2);
        SecurityMenu securityMenu3 = new SecurityMenu();

        securityMenu3.setDisplayName(
                "查看");
        securityMenu3.setIcon(
                "com/galileo/netbeans/module/views.png");
        securityMenu3.setPosition(
                400);
        securityMenus.add(securityMenu3);
        SecurityMenu securityMenu4 = new SecurityMenu();

        securityMenu4.setMenuPath(
                "Actions/Window/org-netbeans-core-io-ui-IOWindowAction");

        securityMenu4.setDisplayName(
                "输出");
        securityMenu4.setOriginalFile(
                "Actions/Window/org-netbeans-core-io-ui-IOWindowAction.instance");
        securityMenu4.setPosition(
                500);
        securityMenus.add(securityMenu4);


        SecurityMenu securityMenu5 = new SecurityMenu();

        securityMenu5.setDisplayName(
                "收藏夹");
        securityMenu5.setIcon(
                "com/galileo/netbeans/module/favorites.png");
        securityMenu5.setPosition(
                600);
        securityMenus.add(securityMenu5);
        
        SecurityMenu securityMenu6 = new SecurityMenu();
        securityMenu6.setMenuPath(
                "Actions/Window/org-netbeans-modules-favorites-View");
        securityMenu6.setDisplayName(
                "查看收藏夹");
        securityMenu6.setOriginalFile(
                "Actions/Window/org-netbeans-modules-favorites-View.instance");
        securityMenu6.setPosition(
                700);
        securityMenus.add(securityMenu6);

        
        
        
        return securityMenus;
    }
}
